package com.xdkj.model.dao.impl;

import com.xdkj.commons.util.RunnerUtil;
import com.xdkj.model.dao.TEmployeeDao;
import com.xdkj.model.entity.TEmployee;
import com.xdkj.model.vo.TEmployeeVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * ClassName TEmployeeDaoImpl
 * Description:
 *
 * @Author:一尘
 * @Version:1.0
 * @Date:2021-11-03-10:55
 */
public class TEmployeeDaoImpl implements TEmployeeDao {
    private QueryRunner runner = RunnerUtil.getRunner();

    /**
     * 查询所有
     *
     * @return {@link List}<{@link TEmployeeVo}>
     */
    @Override
    public List<TEmployeeVo> queryAll() {
        List<Map<String, Object>> list = null;
        List<TEmployeeVo> employeeVoList = new ArrayList<>();
        // 多表查询
        try {
            list = runner.query("select em.id,em.name,em.gender,em.specialty,em.workId,em.birthday,em.email,em.idCard,em.wedlock,em.nativePlace,em.phone,em.address,em.school,em.engageForm,em.tiptopDegree,em.beginDate,em.workState,em.conversionTime,em.beginContract,em.conversionTime,em.endContract,em.contractTerm,em.workAge,dept.name as deptname ,nation.name as nationname,position.`name` as positionname,jobtitle.name as jobname,politic.`name` as politicname\n" +
                    "from t_employee em \n" +
                    "join t_department dept on em.departmentId = dept.id \n" +
                    "join t_nation  nation on em.nationId = nation.id\n" +
                    "join t_position position on em.positionId = position.id\n" +
                    "join t_jobtitle jobtitle on em.jobTitleId = jobtitle.id\n" +
                    "join t_politicsstatus  politic on em.politicsStatusId = politic.id\n", new MapListHandler());
            for (Map<String, Object> map : list) {
                TEmployeeVo employeeVo = new TEmployeeVo();
                // 赋值对象的属性
                BeanUtils.copyProperties(employeeVo, map);
                employeeVoList.add(employeeVo);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return employeeVoList;
    }

    /**
     * 分页查询
     *
     * @param page  页面
     * @param limit 限制
     * @return {@link List}<{@link TEmployeeVo}>
     */
    @Override
    public List<TEmployeeVo> queryByPage(int page, int limit) {
        List<Map<String, Object>> list = null;
        List<TEmployeeVo> employeeVoList = new ArrayList<>();
        // 多表查询
        try {
            list = runner.query("select em.id,em.name,em.gender,em.specialty,em.workId,em.birthday,em.email,em.idCard,em.wedlock,em.nativePlace,em.phone,em.address,em.school,em.engageForm,em.tiptopDegree,em.beginDate,em.workState,em.conversionTime,em.beginContract,em.conversionTime,em.endContract,em.contractTerm,em.workAge,dept.name as deptname ,nation.name as nationname,position.`name` as positionname,jobtitle.name as jobname,politic.`name` as politicname\n" +
                    "from t_employee em \n" +
                    "join t_department dept on em.departmentId = dept.id \n" +
                    "join t_nation  nation on em.nationId = nation.id\n" +
                    "join t_position position on em.positionId = position.id\n" +
                    "join t_jobtitle jobtitle on em.jobTitleId = jobtitle.id\n" +
                    "join t_politicsstatus  politic on em.politicsStatusId = politic.id\n limit ?,?", new MapListHandler(), page, limit);
            for (Map<String, Object> map : list) {
                TEmployeeVo employeeVo = new TEmployeeVo();
                // 赋值对象的属性
                BeanUtils.copyProperties(employeeVo, map);
                employeeVoList.add(employeeVo);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return employeeVoList;
    }

    /**
     * 删除通过id
     *
     * @param id id
     * @return boolean
     */
    @Override
    public boolean deleteById(int id) {
        int result = 0;
        try {
            result = runner.update("delete from t_employee where id = ?", id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result > 0 ? true : false;
    }

    /**
     * 更新员工
     *
     * @param employee 员工
     * @return boolean
     */
    @Override
    public boolean updateEmployee(TEmployee employee) {
        int result = 0;
        try {
            result = runner.update("update t_employee set name=?,gender=?,birthday=?,idcard=?,wedlock=?,nationid=?," +
                            "nativeplace=?,politicsstatusid=?,email=?,phone=?,address=?,departmentid=?,jobtitleid=?,positionid=?,engageform=?," +
                            "tiptopdegree=?,specialty=?,school=?,begindate=?,workid=?,conversiontime=?,begincontract=?,endcontract=? where id =?",
                    employee.getName(), employee.getGender(), employee.getBirthday(), employee.getIdcard(), employee.getWedlock(),
                    employee.getNationid(), employee.getNativeplace(), employee.getPoliticsstatusid(), employee.getEmail(), employee.getPhone(), employee.getAddress(),
                    employee.getDepartmentid(), employee.getJobtitleid(), employee.getPositionid(), employee.getEngageform(),
                    employee.getTiptopdegree(), employee.getSpecialty(), employee.getSchool(), employee.getBegindate(),
                    employee.getWorkid(), employee.getConversiontime(), employee.getBegincontract(), employee.getEndcontract(), employee.getId()
            );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result > 0 ? true : false;
    }
}
